'use strict';
const Controller = require('egg').Controller;
const fs = require("fs")
const path = require("path")
class UserController extends Controller {
    async choseImg(){
        const { ctx } = this
        let id = ctx.cookies.get("userid")
        let imgs = ctx.request.files[0]
        let oldPath = imgs.filepath
        let newPath = __dirname + "/../public/img/uploadImg/" + path.basename(imgs.filepath)
        fs.copyFileSync(oldPath, newPath)
        fs.unlinkSync(oldPath)
        let img = "/img/uploadImg/" + path.basename(imgs.filepath)
        let data = ctx.service.user.uploadImg(img,id)
        ctx.body = data
    }
    async getUserinfo(){
        const { ctx } = this
        let id = ctx.cookies.get("userid")
        let data = await ctx.service.user.getUserinfo(id)
        ctx.body = data
    }
    async keepUserinfo(){
        const { ctx } = this 
        let id = ctx.cookies.get("userid")
        let req = ctx.request.query
        let data = await ctx.service.user.keepUserinfo(req,id)
        ctx.body = data
    }
    async myOrder(){
        const { ctx } = this 
        let req = ctx.request.query
        // console.log(req.state)
        let id = ctx.cookies.get("userid")
        let data = await ctx.service.user.myOrder(id,req)
        ctx.body = data
    }
    async getAccount(){
        const { ctx } = this 
        let id = ctx.cookies.get("userid")
        let data = await ctx.service.user.getAccount(id)
        ctx.body = data
    }
    async canclePay(){
        const { ctx } = this 
        let id = ctx.cookies.get("userid")
        let goodsId = JSON.parse(ctx.request.query.goodsIdArr)
        // console.log(goodsId)
        let data = await ctx.service.user.canclePay(id,goodsId)
        ctx.data = data
    }
    async completePay(){
        const { ctx } = this 
        let id = ctx.cookies.get("userid")
        let goodsId = JSON.parse(ctx.request.query.goodsIdArr)
        let payTime = ctx.request.query.payTime
        let data = await ctx.service.user.completePay(id,goodsId,payTime)
        ctx.body = data
    }
    async modBalance(){
        const { ctx } = this 
        let id = ctx.cookies.get("userid")
        let balance = ctx.request.query.balance
        let data = ctx.service.user.modBalance(balance,id)
        ctx.body =data
    }
}

module.exports = UserController;